Network = {}

Network.SERVER_LOGIN =                  1                                
Network.SERVER_HALL =                   2                                 
Network.SERVER_GAME =                   3                                 


Network.ConnectType = {
 {addr="127.0.0.1",port=8800},
 {addr="127.0.0.1",port=8800},
 {addr="127.0.0.1",port=8800},
}							 

function Connect(wConnectID,ip,port)
   dv.LuaConnector:GetInstance(wConnectID):Connect(ip,port)
end

function GetConnectState(wConnectID)
   dv.LuaConnector:GetInstance(wConnectID):GetConnectState()
end

function CloseSocket(wConnectID)
   dv.LuaConnector:GetInstance(wConnectID):CloseSocket()
end

function SendData(wConnectID,wMainCmdID,wSubCmdID)
   if (GetConnectState(wConnectID)~= 1) then
      CloseSocket(wConnectID)
      Connect(wConnectID,Network.ConnectType[wConnectID].addr,Network.ConnectType[wConnectID].port)
   end
   dv.LuaConnector:GetInstance(wConnectID):SendData(wMainCmdID,wSubCmdID)
end

function SendData(wConnectID,wMainCmdID,wSubCmdID,packout)
   if (GetConnectState(wConnectID)~= 1) then
       CloseSocket(wConnectID)
       Connect(wConnectID,Network.ConnectType[wConnectID].addr,Network.ConnectType[wConnectID].port)
   end
   dv.LuaConnector:GetInstance(wConnectID):SendData(wMainCmdID,wSubCmdID,packout)
end

function OnConnectResponse(wConnectID,wState)
   print("===OnConnectResponse==="..wConnectID.."====wState=="..wState)
   tools.Event:trigger(ds.Account.evtConnectResponse, wConnectID,wState)
end

function OnReceiveData(wConnectID,wMainCmdID,wSubCmdID,packet)
    print("===OnReceiveData==wConnectID="..wConnectID.."==wMainCmdID=="..wMainCmdID.."==wSubCmdID==="..wSubCmdID)

    local evtName 
    if (wConnectID == Network.SERVER_LOGIN and wMainCmdID == Cmd.MAIN_HALL_MSG_LOGIN  ) then -- 用户登录 
        evtName = ds.Account.evtMainHallLogin
    elseif (wConnectID == Network.SERVER_LOGIN and wMainCmdID == Cmd.MAIN_MSG_USER  ) then   -- 公告
        evtName = ds.Launcher.evtMainUserLauncher
    end
   
    if evtName then
       tools.Event:trigger(evtName, wSubCmdID , packet)
    else
       print("==error=OnReceiveData==wConnectID="..wConnectID.."==wMainCmdID=="..wMainCmdID.."==wSubCmdID==="..wSubCmdID) 
    end
    
end

